package com.cat.binaryAnswer;

import java.util.Arrays;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/successful-pairs-of-spells-and-potions/description/
 * @create 2025/8/9 19:11
 * @since JDK17
 */

public class Solution02 {
    public int[] successfulPairs(int[] spells, int[] potions, long success) {
        int n = spells.length, cnt, m = potions.length;
        int[] ans = new int[n];
        Arrays.sort(potions);   // 给药水排序
        for (int i = 0; i < n; i++) {
            cnt = f(((long) spells[i]), potions, success);
            ans[i] = cnt == -1 ? -1 : m - cnt + 1;
        }
        return ans;
    }

    public int f(long x, int[] potions, long success) {
        int l = -1, r = potions.length, mid;
        while (l + 1 < r) {
            mid = l + (r - l) / 2;
            if (x * potions[mid] >= success) {
                r = mid;
            } else {
                l = mid;
            }
        }
        return r == potions.length ? -1 : r;
    }
}
